﻿using System;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Dimok.Data.Database;
using Dimok.Web.UI;

namespace Data.DataSources {
	public class ProfileFilterGroupsDataSource : ObjectDataSourceBase {
		static ProfileCommon Profile(string UserName) { return new ProfileCommon().GetProfile(UserName); }
		#region FilterRow
				static GridFilterDS.FilterRow FilterRow(string UserName, string FilterName, Control control) {
			return FilterRow(Profile(UserName), FilterName, control);
		}
				static GridFilterDS.FilterRow FilterRow(ProfileCommon profile, string FilterName, Control control) {
			GridFilterDS.FilterRow rowFilter = profile.Filters.Filter.FindByNamePageType(FilterName,PageBase.FilterPageType(control));
			if (rowFilter == null) throw new ApplicationException("User [" + profile.UserName + "] does not have filter [" + FilterName + "]");
			return rowFilter;
		}

#endregion
		[Bindable(true)]
		public string FilterName { set { SetSelectParameter("FilterName", value); } }
		public IEnumerable SelectEx(string UserName,string FilterName) {
			if (UserName + "" == "") return new GridFilterDS().FilterGroups.DefaultView;
			if (FilterName + "" == "") return new GridFilterDS().FilterGroups.DefaultView;
			GridFilterDS.FilterRow rowFilter = FilterRow(UserName, FilterName, this);
			GridFilterDS.FilterGroupsRow[] rowsFG = rowFilter.GetFilterGroupsRows();
			if (EmtyRowPolicy == Dimok.Data.DataSources.EmptyRowPolicyType.OnlyIfEmpty && rowsFG.Length == 0) {
				((GridFilterDS)rowFilter.Table.DataSet).FilterGroups.AddFilterGroupsRow("", "",PageBase.FilterPageType(this));
				rowsFG = rowFilter.GetFilterGroupsRows();
			}
			return rowsFG;
		}
		public void DeleteEx(string UserName, string FilterName,string GroupName) {
			ProfileCommon p = Profile(UserName);
			GridFilterDS.FilterGroupsRow rowFG = p.Filters.FilterGroups.FindByFilterNameGroupNamePateType(FilterName,GroupName,PageBase.FilterPageType(this));
			if( rowFG == null )throw new ApplicationException(string.Format("User {0} does not have a filter {1} assigned to group {2}",UserName,FilterName,GroupName));
			rowFG.Delete();
			rowFG.Table.AcceptChanges();
			p.Save();
		}
		public void InsertEx(string UserName, string FilterName, string GroupName) {
			InsertEx(UserName, FilterName, GroupName, this);
		}
		public static void InsertEx(string UserName, string FilterName, string GroupName,Control control) {
			ProfileCommon p = Profile(UserName);
			GridFilterDS.FilterRow rowFilter = FilterRow(p,FilterName,control);
			p.Filters.FilterGroups.AddFilterGroupsRow(FilterName, GroupName,PageBase.FilterPageType(control));
			p.Filters.AcceptChanges();
			p.Save();
		}
	}
}